<?php

namespace App\Admin\Controllers;

use App\Models\Test_config;
use App\Models\Test_report;
use App\Models\Squad;
use App\Models\Student_test_score;
use App\Models\Student;
use Encore\Admin\Auth\Database\Administrator;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;
use App\Admin\Extensions\ExcelExpoter;
use Maatwebsite\Excel\Facades\Excel;
use Encore\Admin\Show;
use Illuminate\Http\Request;
use DB;

class Test_reportController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header(trans('admin.test_report'));
            $content->description(trans('admin.list'));

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header(trans('admin.test_report'));
            $content->description(trans('admin.edit'));

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header(trans('admin.test_report'));
            $content->description(trans('admin.create'));

            $content->body($this->form());
        });
    }

    public function show($id, Content $content)
    {
        return $content
            ->header(trans('admin.test_report'))
            ->description(trans('admin.detail'))
            ->body(Admin::show(Test_report::findOrFail($id), function (Show $show) {
                $show->panel()->tools(function ($tools){
                    $tools->disableDelete();
                    $tools->disableEdit();
                    //$tools->disableList();
                });
                $show->name(trans('admin.test_name'));
                $show->squad()->name(trans('admin.squad'));
                $show->test_config()->name(trans('admin.test_name'));
                $show->test_batch('批次');
                $show->desc(trans('admin.test_desc'));
                
                $show->student_test_scores('成绩单', function ($line) {
                    $line->setResource('/admin/test_score_enter');
                    $line->id(trans('admin.ID'))->sortable();
                    $line->student_id(trans('admin.en_name'))->display(function($student_id){
                        return Student::find($this->student_id)->en_name;
                    });
                    $line->test_score(trans('admin.test_score'))->editable();
                    $line->test_desc(trans('admin.test_desc'))->style('width:600px;word-break:break-all;')->editable('textarea');
                    $line->disableCreateButton();//禁用创建按钮
                    $line->disablePagination();//禁用分页
                    $line->disableFilter();//禁用筛选
                    //禁用批量操作
                    $line->tools(function ($tools) {
                        $tools->batch(function ($batch) {
                            $batch->disableDelete();
                        });
                    });

                    $line->actions(function ($actions) {
                        $actions->disableView();
                        $actions->disableDelete();
                        //$actions->disableEdit();
                        $actions->disableView();
                    });
                    $filename="成绩单";
                    $line->exporter(new ExcelExpoter($line,$filename));
                });
        }));
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(Test_report::class, function (Grid $grid) {
            $squads = Squad::where('class_teacher','=',Admin::user()->id)->pluck('id')->toArray();

            $grid->model()->wherein('squad_id', $squads);
            $grid->model()->orderBy('id', 'desc');
            $grid->id(trans('admin.ID'))->sortable();
            $grid->squad_id(trans('admin.squad'))->display(function($squad_id){
                return Squad::find($this->squad_id)->name;
            });
            
            $grid->test_id(trans('admin.test_name'))->display(function($test_id){
                return Test_config::find($this->test_id)->name;
            });
            $grid->test_batch(trans('admin.test_batch'));
            $grid->name(trans('admin.test_report_name'))->sortable();
            $grid->desc(trans('admin.test_desc'))->display(function($desc) {
                return str_limit($desc, 30, '...');
            });

            $grid->actions(function ($actions) {
                $actions->disableDelete();//禁用删除按钮
            });
            //禁用导出按钮
            $grid->disableExport();
            $filename="成绩记录";
            $grid->exporter(new ExcelExpoter($grid,$filename));
            
            $grid->tools(function ($tools) { 
                //关闭批量删除 
                $tools->batch(function ($batch) { 
                    $batch->disableDelete(); 
                });
            });

            //筛选
            $grid->filter(function ($filter) {
                $filter->column(1/2, function ($filter) {
                    $filter->disableIdFilter();
                    $filter->equal('squad_id', trans('admin.squad'))->select(Squad::where('class_teacher','=',Admin::user()->id)->get()->pluck('name', 'id'))->load('test_id', '/api/get_squad_test');
                    $filter->equal('test_batch',trans('admin.test_batch'))->integer();
                });
                $filter->column(1/2, function ($filter) {
                    $filter->equal('test_id', trans('admin.test_name'))->select(Test_config::where('status','=','0')->get()->pluck('name', 'id'));
                    $filter->equal('name',trans('admin.test_report_name'));
                    
                });
            });
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    { 
        $form = new Form(new Test_report());

        $form->display('id', 'ID');
        $form->text('name',trans('admin.test_report_name'))->rules('required')->addElementLabelClass('must-input');
        $form->select('squad_id','要生成成绩的班级')->options(Squad::where('class_teacher','=',Admin::user()->id)->get()->pluck('name','id'))->load('test_id', '/api/get_squad_test')->rules('required')->addElementLabelClass('must-input');
        $form->select('test_id','要录入成绩的测试')->options(function ($test_id){
           $data = Test_config::find($test_id);
           return [$data['id']=>$data['name']];
        });
        // $form->select('test_id','要录入成绩的测试')->options(array(0 =>'请选择测试'))->rules('required');
        $form->number('test_batch', '测试批次')->max(200)->min(1)->default(1)->rules('required|integer')->addElementLabelClass('must-input');
        $form->textarea('desc',trans('admin.test_desc'))->rows(10);
        //去掉添加/编辑表单的重置按钮
        $form->disableReset();
        $form->tools(function (Form\Tools $tools) {
            // 去掉`列表`按钮
            $tools->disableList();
            // 去掉`删除`按钮
            $tools->disableDelete();
            // 去掉`查看`按钮
            $tools->disableView();
        });
        //成绩单生成后，自动生成对应的学生成绩单，学生成绩默认0分
        $form->saved(function(Form $form){
            $test_report_id = $form->model()->id;
            $squad_id = $form->model()->squad_id;
            $student_ids = Student::where('squad_id','=',$squad_id)->pluck('id')->toArray();
            $data = [];
            for ($i=0; $i <count($student_ids) ; $i++) { 
                $data[] = [
                    'test_report_id' =>$test_report_id,
                    'student_id' =>$student_ids[$i],
                    'test_score' =>0
                ];
            }
            $res = Student_test_score::insert($data);
        });
        return $form;
    }
}
